package com.md.base.db;

import com.md.util.ObjectUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.expression.HexValue;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 刘骄阳
 * 2025-06-14 2:19
 */
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
public class CustomExpression extends ASTNodeAccessImpl implements Expression {
  private List<? extends ExpressionParamBase> expressions;

  private Expression originalExpression;

  public CustomExpression(final Expression originalExpression) {
    this.originalExpression = originalExpression;
  }

  @Override
  public void accept(final ExpressionVisitor expressionVisitor) {
    if (ObjectUtil.isEmpty(expressions)) {
      return;
    }
    Expression rightWhere = new HexValue(expressions.stream().map(ExpressionParamBase::parse)
      .collect(Collectors.joining("")));
    expressionVisitor.visit(new AndExpression(originalExpression, rightWhere));
  }

}
